package com.base_data_handler.surfer;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;


/**
 * 将经纬度转换成屏幕像素坐标
 *
 */

public class LocationTransfer {
	 public static int  maxX ;
	 public static int  maxY ;
	    public static double minLongitude = ReadFile.longitude.get(0);
	    public static double maxLatitude = ReadFile.latitude.get(0);

	    public static  int screenWidth = 960;
	    public static  int screenHeight = 768;
	    // change longitute and latitude to view point
	    public  ArrayList<Point> makeScreenPoint(List<Double> longitude,List<Double> latitude) {
	    	ArrayList<Point> point = new ArrayList<Point>();
	    	minLongitude=longitude.get(0);
	    	maxLatitude=latitude.get(0);
	        for (int i = 0; i < longitude.size(); i++) {
	            minLongitude = Math.min(minLongitude, longitude.get(i));
	            maxLatitude = Math.max(maxLatitude, latitude.get(i));
	        }

	        /************************************************************/
	        //获得屏幕大小
//	        Dimension   screensize   =   Toolkit.getDefaultToolkit().getScreenSize();
//	        int screenWidth = (int)screensize.getWidth();
//	        int screenHeight = (int)screensize.getHeight();

	        //求距离参照点最远的点
	        double maxDistance = distanceLongLat(minLongitude, ReadFile.longitude.get(0), 
	                maxLatitude, ReadFile.latitude.get(0));
	        for (int i = 0; i < ReadFile.longitude.size(); i++) {
	            maxDistance = Math.max(maxDistance, distanceLongLat(minLongitude, 
	                ReadFile.longitude.get(i), maxLatitude, ReadFile.latitude.get(i)));
	        }
	        //转换比率
	        double ratio = 2*maxDistance / Math.sqrt((screenWidth * screenWidth
	                + screenHeight * screenHeight));

	        //将经纬度转换成屏幕坐标   
	        int x;
	        int y;
	        for (int i = 0; i < ReadFile.longitude.size(); i++) {
	            Point p = new Point();          

	             p.y= (int) (distanceLongLat(minLongitude,
	                    ReadFile.longitude.get(i), maxLatitude, maxLatitude) / ratio);
	           
	            p.x = (int) (distanceLongLat(minLongitude, minLongitude,
	                    maxLatitude, ReadFile.latitude.get(i)) / ratio);
//                System.out.println("x="+ p.x+"y="+ p.y);
	            x= p.x;
                y= p.y;
                maxX=Math.max(x, p.x);
                maxY=Math.max(y, p.y);
	            point.add(p);
	        }
	        System.out.println("x="+ maxX+"y="+ maxY);
			return point;
	    }
	    //简化距离公式法
	    public static double distanceLongLat( double lng1,double lng2,double lat1, 
	        double lat2) {
	         double dx = lng1 - lng2; // 经度差值
	         double dy = lat1 - lat2; // 纬度差值
	         double b = (lat1 + lat2) / 2.0; // 平均纬度
	         double Lx = dx * Math.PI/180 * 6367000.0 * Math.cos(b*Math.PI/180); // 东西距离
	         double Ly = 6367000.0 * dy*Math.PI/180; // 南北距离
	         return Math.sqrt(Lx * Lx + Ly * Ly);  // 用平面的矩形对角距离公式计算总距离
	        }   
	    
	    public static void main(String[] args) {
	    	long start=System.currentTimeMillis();
//			makeScreenPoint();
	    	System.out.println("耗时："+(System.currentTimeMillis()-start)/1000);
	    	
		}
	    
}
